﻿using System;
//using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DayZAntiHax;
using System.Reflection;

namespace DayZAntiHax
{
    public partial class MainForm : Form
    {
        TextWriter _writer = null;
        //static int s_iNumPasses = 1;
        static Timer ExecuteTime = new Timer();
        static Timer OneSecTick = new Timer();
        Process Process1 = new Process();
        
        private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
        {
            ExecuteTime.Stop();
            OneSecTick.Stop();
            Process1.Execute(null);
            progressBar1.Value = 0;
            ExecuteTime.Start();
            OneSecTick.Start();
        }

        void OneSecTick_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value != progressBar1.Maximum)
            {
                progressBar1.Value++;
            }
            else
            {
                OneSecTick.Stop();
            }
        }

        private void txtConsole_TextChanged(object sender, EventArgs e)
        {
        }

        public MainForm()
        {
            InitializeComponent();
        }
        
        private void MainForm_Load(object sender, EventArgs e)
        {
            Assembly assem = Assembly.GetEntryAssembly();
            AssemblyName assemName = assem.GetName();
            Version ver = assemName.Version; 
            
            Text = "DayZ AntiHax v " + ver.ToString();
            ExecuteTime.Tick += new EventHandler(TimerEventProcessor);
            OneSecTick.Tick += new EventHandler(OneSecTick_Tick);

            progressBar1.Maximum = 60;
            label1.Text = "Initializing...";
            // Instantiate the writer
            _writer = new TextBoxStreamWriter(txtConsole);
            // Redirect the out Console stream
            Console.SetOut(_writer);

            if (Process1.Initialize())
            {
                Console.WriteLine("Parsing frequency set to {0} second(s)", Process1.ParsingFrequency);
                Console.WriteLine("Flood checking {0}", Process1.ShouldCheckForFlooding ? "enabled" : "disabled");

                Console.WriteLine(String.Empty);
                Console.WriteLine("Initialization complete. Waiting to execute first pass...");
                Console.WriteLine(String.Empty);
                ExecuteTime.Interval = Convert.ToInt32(Process1.ParsingFrequency*1000);
                OneSecTick.Interval = 1000;

                label1.Text = "Online";

                ExecuteTime.Start();
                OneSecTick.Start();
            }
            else
            label1.Text = "**Error**";
        }
    }
}